const request = options => {
	const PATH = require(process.env.ENV_TYPE).default;//详见env
    const TOKEN = uni.getStorageSync('accessToken');
	const HEADER = {
		// 'content-type': 'application/json',
	    'content-type': 'application/x-www-form-urlencoded'
	}; 
	
	let headerObj = HEADER;
    if (TOKEN) {
        headerObj = {
            'token': TOKEN,
            ...HEADER
        }
    }
	if (options.isloading) {//如果传入loading为true,则显示loadding
		uni.showLoading({
			title: '加载中',
			mask: true
		})
	}

    return new Promise((resolve, reject) => {
        uni.request({
            url: PATH.baseURL + options.url,
            method: options.method || 'GET',
            header: headerObj,
            data: options.data || {},
            dataType: 'json',
            success: res => {
				uni.hideLoading()
                // console.log('res-->:', res)
				if(res.statusCode == 200){
					if(res.data.success == false){
						 reject(res.data.message);
					}
				}else{
					 reject("接口错误："+res.statusCode);
				}
				
                resolve(res);
            },
            fail: err => {
                reject(err);
            },
            catch: (e) => {
                console.log(e);
            }
        });
    });
};


export const GET = (url,data,isloading) => {
    return request({
        method: 'GET',
        url,
        data,
		isloading
    })
}

export const POST = (url,data,isloading) => {
    return request({
        method: 'POST',
        url,
        data,
		isloading
    })
}